package cat.inkubator.plugin4j.xml;

import java.io.File;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.JAXBException;

/**
 *
 * @author gato
 */
@XmlRootElement
public class Plugins {
    
    private ArrayList<Plugin> plugins = new ArrayList();
    
    public Plugins(){}
    
    public Plugins( ArrayList<Plugin> plugins ){
        this.plugins = plugins;
    }
    
    public String toXML() throws JAXBException{
        Xmlizer xml = new Xmlizer( Plugins.class );
        
        return xml.toXml(this);
    }
    
    public void fromXML( String xmlString ) throws JAXBException{
        Xmlizer xml =  new Xmlizer(Plugins.class);
        
        Plugins pluginsXml = (Plugins) xml.fromXml(xmlString);
        
        this.plugins = pluginsXml.getPlugins();
    }
    
    public boolean toFile( String fileName ){
        try{
            File file = new File(fileName);
            Xmlizer xml = new Xmlizer( Plugins.class );

            xml.xmlToFile(this, file);
        }catch( Exception e ){
            e.printStackTrace();
            return false;
        }
        
        return true;
    }
    
    public ArrayList<Plugin> getPlugins() {
        return plugins;
    }

    public void setPlugins(ArrayList<Plugin> plugins) {
        this.plugins = plugins;
    }

}
